﻿namespace GoodAdmin.Core;

public class UtilMethod
{
    /// <summary>
    /// 根据Id递归获取下级
    /// </summary>
    /// <typeparam name="T">继承自BaseEntityHasPid的实体类型</typeparam>
    /// <param name="list">指定查找范围</param>
    /// <param name="result">返回结果</param>
    /// <param name="idList">父级组织id列表</param>
    /// <returns></returns>
    public static List<T> GetChild<T>(List<T> list, List<T> result, List<long> idList) where T : BaseEntityHasPid
    {
        if (idList.Count == 0) return result;
        var childs = list.Where(it=>idList.Contains(it.ParentId)).ToList();
        idList = childs.Select(it=>it.Id).ToList();
        result.AddRange(childs);
        return GetChild(list, result, idList);
    }

    /// <summary>
    /// 递归获取下级列表
    /// </summary>
    /// <typeparam name="T">实体类型</typeparam>
    /// <typeparam name="TKey"></typeparam>
    /// <param name="list">指定查找范围</param>
    /// <param name="result">返回结果</param>
    /// <param name="keyList">需查询下级信息的Id值</param>
    /// <param name="whereExpression">查询条件</param>
    /// <param name="selectExpression">获取</param>
    /// <returns></returns>
    public List<T> GetChild<T,TKey>(List<T> list, List<T> result, List<TKey> keyList, Func<T, bool> whereExpression,Func<T, TKey> selectExpression) 
    {
        if (keyList.Count == 0) return result;
        var childs = list.Where(whereExpression).ToList();
        keyList = childs.Select(selectExpression).ToList();
        result.AddRange(childs);
        return GetChild(list, result, keyList, whereExpression, selectExpression);
    }

    /// <summary>
    /// 将实体字段及对应的注释转换为字典
    /// </summary>
    /// <typeparam name="Entity"></typeparam>
    /// <returns></returns>
    public static Dictionary<string,string> GetEntityMetadataToDic<Entity>()
    {
        var entityMetadataDic = new Dictionary<string, string>();

        var entityType = typeof(Entity);
        var properties = entityType.GetProperties();

        foreach (var property in properties)
        {
            var fieldName = property.Name;

            var displayAttribute = property.GetCustomAttribute<DisplayAttribute>();
            var comment = fieldName;
            if (displayAttribute != null)
            {
                comment = displayAttribute.Name;
            }

            entityMetadataDic.Add(fieldName, comment);
        }

        return entityMetadataDic;
    }

    /// <summary>
    /// 将实体字段及对应的注释转换为List
    /// </summary>
    /// <returns></returns>
    public static List<EntityMetadata> GetEntityMetadataToList<Entity>()
    {
        var entityMetadataList = new List<EntityMetadata>();

        var entityType = typeof(Entity);
        var properties = entityType.GetProperties();

        foreach (var property in properties)
        {
            var metadata = new EntityMetadata
            {
                FieldName = property.Name
            };

            var displayAttribute = property.GetCustomAttribute<DisplayAttribute>();
            if (displayAttribute != null)
            {
                metadata.Comment = displayAttribute.Name;
            }

            entityMetadataList.Add(metadata);
        }
        return entityMetadataList;
    }
}